package com.ecloud.scheduler.trigger;

import org.quartz.CronScheduleBuilder;
import org.quartz.Trigger;
import org.quartz.TriggerBuilder;
import org.springframework.stereotype.Component;

/**
 * @author Squbi
 * @date 2021/7/19 11:22
 */
@Component
public class CronTriggerFactory implements TriggerFactory {
    private int created;

    public CronTriggerFactory() {
        this.created = 0;
    }

    @Override
    public Trigger create(String cron) {
        Class<? extends TriggerFactory> clazz = this.getClass();
        created += 1;
        return TriggerBuilder.newTrigger().withIdentity(clazz.getSimpleName() + created, "triggerGroup")
                .startNow()
                .withSchedule(CronScheduleBuilder.cronSchedule(cron)).build();
    }
}
